package com.wc.wsclub.controller;

import com.wc.wsclub.entity.Wsn;
import com.wc.wsclub.service.WsService;
import com.wc.wsclub.service.WsServiceimpt;
//import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.xml.transform.Result;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller

public class WScontroller {

     @Autowired
     private  WsService service;
   private WsServiceimpt wsServiceimpt;

        @CrossOrigin(origins = "http://localhost:63342")
        @PostMapping("/login")
        public String login(@RequestParam String username, @RequestParam String password, RedirectAttributes redirectAttributes) {
            Wsn wsn = service.login(username, password);
            if (wsn != null) {
                return "redirect:Menu.html";
            } else {
                redirectAttributes.addFlashAttribute("error", "有问题，密码或名称");
                return "redirect: Login.html";
            }
        }

    @CrossOrigin(origins = "http://localhost:63342")
    @GetMapping("/findall")
     @ResponseBody
      public List<Map<String,Object>>findAll(){
         List<Map<String,Object>> list =service.findAll();
         return list;
}
    @CrossOrigin(origins = "http://localhost:63342")
@PostMapping("/wsn/Add")
@ResponseBody
public int addWsn(Wsn wsn){
         int res=service.addWsn(wsn);
         return res;
}
@PostMapping("/delete")
@ResponseBody
    public int deleteWsn(@PathVariable int id){
         System.out.println(id);
         int res = service.deleteWsn(id);
         return res;
}
@PostMapping("/Change")
@ResponseBody
    public int updateWsn(Wsn wsn){
         System.out.println(wsn.getId());
         int isHas=service.isHasWsn(wsn.getId());
         int res=0;
         if(isHas==1){
             res=service.updateWsn(wsn);
         }
         return res;
}
@PostMapping("/import")
    public String handleFileUpload(@RequestParam("file")MultipartFile file){
         if(file.isEmpty()){
             return "选择一个文件上传";
         }
         try{
             //获取文件名并保存到服务器
             String fileName =file.getOriginalFilename();
             String filePath="path/to/save/files/"+fileName;//设置保存路径
             File dest=new File(filePath);
             file.transferTo(dest);
             return "文件导入成功";
         }catch (IOException e){
             e.printStackTrace();
             return "导入文件失败";
         }
}

}
